import 'dart:convert';
import 'package:http/http.dart' as http;
import '../util/UserContext.dart';
import '../util/LogUtils.dart';
import '../model/LoanApply.dart';
import '../model/LoanCommission.dart';
import '../model/LoanSituation.dart';
import '../model/LoanAttachment.dart';

class ApiUtils {
  static const int defaultPageLimit = 10;
  static final String apiHost = 'http://39.108.246.130/api';
  static final String serviceHost = 'http://mp.bxjituan.cn:8080/file';

  ///
  /// 登录
  ///
  static void login(
    String account,
    String password, {
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/auth/login";
    String data = "{\"account\": \"$account\", \"password\": \"$password\"}";
    post(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("登录成功");
        LogUtils.debug("accessToken: " + jsonObject["data"]["accessToken"]);
        LogUtils.debug("refershToken: " + jsonObject["data"]["refershToken"]);
        LogUtils.debug("tokenType: " + jsonObject["data"]["tokenType"]);
        LogUtils.debug(
            "expireIn: " + jsonObject["data"]["expireIn"].toString());
        LogUtils.debug("jti: " + jsonObject["data"]["jti"].toString());

        UserContext.setSession(jsonObject["data"]);

        // 成功回调
        if (successCallback != null) {
          successCallback();
        }
      } else {
        LogUtils.debug("登录失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    }, data: data);
  }

  ///
  /// 获取用户信息
  ///
  static void getUserInfo({
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/user/getuserinfo";
    get(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("获取用户信息成功");
        LogUtils.debug("id: " + jsonObject["data"]["id"].toString());
        LogUtils.debug("account: " + jsonObject["data"]["account"]);
        LogUtils.debug("name: " + jsonObject["data"]["name"]);
        LogUtils.debug(
            "accountType: " + jsonObject["data"]["accountType"].toString());
        LogUtils.debug("avatar: " + jsonObject["data"]["avatar"].toString());

        UserContext.setUserInfo(jsonObject["data"]);

        // 成功回调
        if (successCallback != null) {
          successCallback();
        }
      } else {
        LogUtils.debug("获取用户信息失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    });
  }

  ///
  /// 修改密码
  ///
  static void modifyPassword(
    String password,
    String newPassword,
    String confirmPassword, {
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/user/modifypassword";
    String data =
        "{\"password\": \"$password\", \"newPassword\": \"$newPassword\", \"confirmPassword\": \"$confirmPassword\"}";
    post(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("修改密码成功");

        // 成功回调
        if (successCallback != null) {
          successCallback();
        }
      } else {
        LogUtils.debug("修改密码失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    }, data: data);
  }

  ///
  /// 获取TopN大事件
  ///
  static void listEvents({
    int page = 1,
    int limit = defaultPageLimit,
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/article/getevents";
    Map<String, String> params = {"page": "$page", "limit": "$limit"};
    get(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("获取大事件列表成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("获取大事件列表失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    }, params: params);
  }

  ///
  /// 获取用户待办列表
  ///
  static void listUserApply({
    int start = 1,
    int limit = defaultPageLimit,
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/bpm/getmytaskspage";
    Map<String, String> params = {"start": "$start", "limit": "$limit"};
    get(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("获取用户待办列表成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("获取用户待办列表失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    }, params: params);
  }

  ///
  /// 获取客户申请列表
  ///
  static void listMemberApply({
    int page = 1,
    int limit = defaultPageLimit,
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/loan/getloanapplylist";
    Map<String, String> params = {"page": "$page", "limit": "$limit"};
    get(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("获取客户申请列表成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("获取客户申请列表失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    }, params: params);
  }

  ///
  /// 保存车贷申请
  ///
  static void saveLoanApply(
    LoanApply loanApply, {
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/loan/saveloanapply";
    String data = loanApply.toString();
    post(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("保存车贷申请成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("保存车贷申请失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    }, data: data);
  }

  ///
  /// 获取申请车贷详情
  ///
  static void getApplyDetail(
    int id, {
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/loan/getloanapplydetail/$id";
    get(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("获取申请车贷详情成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("获取申请车贷详情失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    });
  }

  ///
  /// 获取车贷综合代理费详情
  ///
  static void getLoanCommission(
    int id, {
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/loanCommission/read/$id";
    get(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("获取车贷综合代理费详情成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("获取车贷综合代理费详情失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    });
  }

  ///
  /// 保存车贷综合代理费
  ///
  static void saveLoanCommission(
    LoanCommission loanCommission, {
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/loanCommission/save";
    String data = loanCommission.toString();
    post(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("保存车贷综合代理费成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("保存车贷综合代理费失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    }, data: data);
  }

  ///
  /// 删除车贷综合代理费
  ///
  static void deleteLoanCommission(
    int id, {
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/loanCommission/delete";
    String data = "{\"id\":\"$id\"}";
    post(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("删除车贷综合代理费成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("删除车贷综合代理费失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    }, data: data);
  }

  ///
  /// 获取情况描述列表
  ///
  static void listLoanSituation(
    int applyId, {
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/loanSituation/getList";
    Map<String, String> params = <String, String>{"applyId": "$applyId"};
    get(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("获取情况描述列表成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("获取情况描述列表失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    }, params: params);
  }

  ///
  /// 保存情况描述
  ///
  static void saveLoanSituation(
    LoanSituation loanSituation, {
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/loanSituation/save";
    String data = loanSituation.toString();
    post(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("保存情况描述成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("保存情况描述失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    }, data: data);
  }

  ///
  /// 获取附件列表
  ///
  static void listAttachments(
    int applyId,
    String types, {
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/loanAttachment/listAttachments";
    Map<String, String> params = <String, String>{
      "applyId": "$applyId",
      "types": "$types"
    };
    get(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("获取附件列表成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("获取附件列表失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    }, params: params);
  }

  ///
  /// 保存附件
  ///
  static void saveLoanAttachment(
    LoanAttachment loanAttachment, {
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/loanAttachment/saveAttachment";
    String data = loanAttachment.toString();
    post(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("保存附件成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("保存附件失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    }, data: data);
  }

  ///
  /// 删除附件
  ///
  static void deleteLoanAttachment(
    int attachmentId, {
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/loanAttachment/deleteAttachment";
    String data = "{\"attachmentId\":\"$attachmentId\"}";
    post(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("删除附件成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("删除附件失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    }, data: data);
  }

  ///
  /// 获取审批意见列表
  ///
  static void listApproves(
    int applyId, {
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/loan/listApproves";
    Map<String, String> params = <String, String>{
      "applyId": "$applyId",
    };
    get(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("获取审批意见列表成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("获取审批意见列表失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    }, params: params);
  }

  ///
  /// 获取经销商政策列表
  ///
  static void listLoanPolicy(
    int partnerId, {
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/loanPolicy/listByPartner/$partnerId";
    get(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("获取经销商政策列表成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("获取经销商政策列表失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    });
  }

  ///
  /// 获取品牌列表
  ///
  static void listBrand({
    int page = 1,
    int limit = defaultPageLimit,
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/masterdata/getcarbrandlist";
    Map<String, String> params = {"page": "$page", "limit": "$limit"};
    get(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("获取品牌列表成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("获取品牌列表失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    }, params: params);
  }

  ///
  /// 获取品牌信息
  ///
  static void getBrandInfo(
    int brandId, {
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/masterdata/getcarbrandinfo";
    get(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("获取品牌信息成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("获取品牌信息失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    });
  }

  ///
  /// 获取车系列表
  ///
  static void listSeries({
    int page = 1,
    int limit = defaultPageLimit,
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/masterdata/getcarserieslist";
    Map<String, String> params = {"page": "$page", "limit": "$limit"};
    get(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("获取车系列表成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("获取车系列表失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    }, params: params);
  }

  ///
  /// 获取车系信息
  ///
  static void getSeriesInfo(
    int carSeriesId, {
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/masterdata/getcarseriesinfo";
    get(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("获取车系信息成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("获取车系信息失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    });
  }

  ///
  /// 获取车型列表
  ///
  static void listCarType({
    int page = 1,
    int limit = defaultPageLimit,
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/masterdata/getcartypelist";
    Map<String, String> params = {"page": "$page", "limit": "$limit"};
    get(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("获取车型列表成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("获取车型列表失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    }, params: params);
  }

  ///
  /// 获取车型信息
  ///
  static void getCarTypeInfo(
    int typeId, {
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/masterdata/getcartypeinfo";
    get(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("获取车型信息成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("获取车型信息失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    });
  }

  ///
  /// 获取用户经销商列表
  ///
  static void listPartner({
    int start = 1,
    int limit = defaultPageLimit,
    String searchKey,
    int status,
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/partner/getpartnerlist";
    Map<String, String> params = {"start": "$start", "limit": "$limit"};
    if (searchKey != null) {
      params.addAll({"searchKey": searchKey});
    }
    if (status != null) {
      params.addAll({"status": "$status"});
    }
    get(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("获取用户经销商列表成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("获取用户经销商列表失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    }, params: params);
  }

  ///
  /// 获取汽车经销商列表
  ///
  static void listCarPartner({
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/partner/getcarpartnerlist";
    get(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("获取汽车经销商列表成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("获取汽车经销商列表失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    });
  }

  ///
  /// 获取借款银行列表
  ///
  static void listLoanBank({
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/treenode/getloanbanklist";
    get(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("获取借款银行列表成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("获取借款银行列表失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    });
  }

  ///
  /// 获取贷款产品列表
  ///
  static void listLoanProduct({
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/treenode/getloanproductlist";
    get(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("获取贷款产品列表成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("获取贷款产品列表失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    });
  }

  ///
  /// 获取合同主体名列表
  ///
  static void listContractSubject({
    Function successCallback,
    Function failCallback,
  }) {
    String url = apiHost + "/treenode/getcontractsubjectlist";
    get(url, (responseText) {
      var jsonObject = json.decode(responseText);
      if (_isSuccess(jsonObject["status"])) {
        LogUtils.debug("获取合同主体名列表成功");

        // 成功回调
        if (successCallback != null) {
          successCallback(jsonObject["data"]);
        }
      } else {
        LogUtils.debug("获取合同主体名列表失败：" + jsonObject["message"]);

        // 失败回调
        if (failCallback != null) {
          failCallback(jsonObject["message"]);
        }
      }
    });
  }

  ///
  /// GET获取HTTP请求
  ///
  static void get(
    String url,
    Function callback, {
    Map<String, String> params,
    Function errorCallback,
  }) async {
    if (params != null && params.isNotEmpty) {
      StringBuffer sb = new StringBuffer("?");
      params.forEach((key, value) {
        sb.write("$key" + "=" + "$value" + "&");
      });
      String paramStr = sb.toString();
      paramStr = paramStr.substring(0, paramStr.length - 1);
      url += paramStr;
    }

    LogUtils.debug("get url: $url");
    Map<String, String> headers = <String, String>{
      "Content-type": "application/json; charset=utf-8"
    };
    if (UserContext.isAccessTokenNotEmpty()) {
      headers.addAll({"Authorization": UserContext.buildAuthorization()});
    }

    try {
      http.Response res = await http.get(url, headers: headers);
      LogUtils.debug("get response: ${res.body}");
      if (callback != null) {
        callback(res.body);
      }
    } catch (e) {
      if (errorCallback != null) {
        errorCallback(e);
      }
    }
  }

  ///
  /// POST提交HTTP请求
  /// data: json字符串
  ///
  static void post(
    String url,
    Function callback, {
    String data,
    Function errorCallback,
  }) async {
    LogUtils.debug("post url: $url");
    LogUtils.debug("post data: $data");

    Map<String, String> headers = <String, String>{
      "Content-type": "application/json; charset=utf-8"
    };
    if (UserContext.isAccessTokenNotEmpty()) {
      headers.addAll({"Authorization": UserContext.buildAuthorization()});
    }

    try {
      http.Response res = await http.post(url, body: data, headers: headers);
      LogUtils.debug("post response: ${res.body}");
      if (callback != null) {
        callback(res.body);
      }
    } catch (e) {
      if (errorCallback != null) {
        errorCallback(e);
      }
    }
  }

  static bool _isSuccess(int status) {
    return status == 200 || status == 0;
  }

  ///
  /// 获取大图片完整URL
  ///
  static String getBigImageUrl(String path) {
    return "$serviceHost/$path";
  }

  ///
  /// 获取小图片完整URL
  ///
  static String getSmallImageUrl(String path) {
    int find = path.lastIndexOf(".");
    String smallPath = path.substring(0, find) + "_s" + path.substring(find);
    return "$serviceHost/$smallPath";
  }
}
